function numIslands(grid) {
    let res = 0;

    let m = grid.length, n = grid[0].length;

    for (let row = 0; row < m; row++) {
        for (let col = 0; col < n; col++) {
            if (grid[row][col] === '1') {
                dfs(row, col);
                res++;
            };
        };
    };


    function dfs(row, col) {
        if (row < 0 || row >= m || col < 0 || col >= n || grid[row][col] === '0') return;

        grid[row][col] = '0';
        dfs(row-1, col);
        dfs(row+1, col);
        dfs(row, col-1);
        dfs(row, col+1);
    };


    return res;
};



// test
const grid = [
    ["1","1","1","1","0"],
    ["1","1","0","1","0"],
    ["1","1","0","0","0"],
    ["0","0","0","0","0"]
  ]
  
  
const res = numIslands(grid);
console.log(res);    // 1